package cn.source.report.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.source.report.entity.ReportParams;
import cn.source.report.mapper.DbVoltageHarmonicsMapper;
import cn.source.report.service.DbVoltageHarmonicsService;
import cn.source.zk.mapper.DbFieldInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DbVoltageHarmonicsServiceImpl implements DbVoltageHarmonicsService {

    /**
     * 电压谐波报表
     */
    //    A相表
    private static final String dbUhraMax = "db_uhramax";
    private static final String dbUhraMin = "db_uhramin";
    private static final String dbUhraAvg = "db_uhraavg";
    private static final String dbUhraTj = "db_uhratj";
    private static final String dbUhraColumn = "Ua";

    //    B相表
    private static final String dbUhrbMax = "db_uhrbmax";
    private static final String dbUhrbMin = "db_uhrbmin";
    private static final String dbUhrbAvg = "db_uhrbavg";
    private static final String dbUhrbTj = "db_uhrbtj";
    private static final String dbUhrbColumn = "Ub";


    //    C相表
    private static final String dbUhrcMax = "db_uhrcmax";
    private static final String dbUhrcMin = "db_uhrcmin";
    private static final String dbUhrcAvg = "db_uhrcavg";
    private static final String dbUhrcTj = "db_uhrctj";
    private static final String dbUhrcColumn = "Uc";


    /**
     * 电流谐波报表
     */
    //    A相表
    private static final String dbIhraMax = "db_ihramax";
    private static final String dbIhraMin = "db_ihramin";
    private static final String dbIhraAvg = "db_ihraavg";
    private static final String dbIhraTj = "db_ihratj";
    private static final String zkCxdz = "zk_cxdz";
    private static final String dbIhraColumn = "Ia";

    //    B相表
    private static final String dbIhrbMax = "db_ihrbmax";
    private static final String dbIhrbMin = "db_ihrbmin";
    private static final String dbIhrbAvg = "db_ihrbavg";
    private static final String dbIhrbTj = "db_ihrbtj";
    private static final String dbIhrbColumn = "Ib";


    //    C相表
    private static final String dbIhrcMax = "db_ihrcmax";
    private static final String dbIhrcMin = "db_ihrcmin";
    private static final String dbIhrcAvg = "db_ihrcavg";
    private static final String dbIhrcTj = "db_ihrctj";
    private static final String dbIhrcColumn = "Ic";


    @Autowired
    private DbVoltageHarmonicsMapper dbVoltageHarmonicsMapper;

    @Autowired
    private DbFieldInfoMapper dbFieldInfoMapper;

    @Override
    public List<Map<String, Object>> selectVoltageHarmonics(ReportParams reportParams) {
        List<Map<String, Object>> resultReport = new ArrayList<>();
        List<String> tableColumn = dbFieldInfoMapper.getTableColumn("zk_cxdz", "ihru");
        tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());

        String condition = "type = " + reportParams.getAlarmType();
        if(StrUtil.isNotBlank(reportParams.getTdGuid())){
            condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
        }

//        A表最大最小平均值
        Map<String, Object> dbUhraMaxMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhraMax, dbUhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhraMinMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhraMin, dbUhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhraAvgMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhraAvg, dbUhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhraTjMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhraTj, dbUhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);


        //        B表最大最小平均值
        Map<String, Object> dbUhrbMaxMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrbMax, dbUhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhrbMinMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrbMin, dbUhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhrbAvgMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrbAvg, dbUhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhrbTjMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrbTj, dbUhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());


        //        C表最大最小平均值
        Map<String, Object> dbUhrcMaxMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrcMax, dbUhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhrcMinMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrcMin, dbUhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhrcAvgMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrcAvg, dbUhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbUhrcTjMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbUhrcTj, dbUhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());

        for (int i = 1; i < 66; i++) {
            Map<String, Object> resultMap = new HashMap<>();
//            格式化查询结构
            String suffix = String.format("%s", i < 10 ? "0" + i : i);
            resultMap.put("sign", String.format("H%s(%s)", i, i == 1 ? "V" : "%"));
//            A相表数据

            Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
            Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get("ihru" + i)) ? (Double) cxdzMap.get("ihru" + i) : 0;
            //            A相表数据
            resultMap.put("aMax", Objects.nonNull(dbUhraMaxMap) ? dbUhraMaxMap.get(dbUhraColumn + suffix) : 0);
            resultMap.put("aMin", Objects.nonNull(dbUhraMinMap) ? dbUhraMinMap.get(dbUhraColumn + suffix) : 0);
            resultMap.put("aAvg", Objects.nonNull(dbUhraAvgMap) ? dbUhraAvgMap.get(dbUhraColumn + suffix) : 0);
            resultMap.put("aTj", Objects.nonNull(dbUhraTjMap) ? dbUhraTjMap.get(dbUhraColumn + suffix) : 0);
            resultMap.put("aCxdz", cxdzStr);
            resultMap.put("aIsNormal", Double.parseDouble(String.valueOf((resultMap.get("aTj")))) > Double.parseDouble(String.valueOf(resultMap.get("aCxdz"))) ? 0 : 1);
            //            B相表数据
            resultMap.put("bMax", Objects.nonNull(dbUhrbMaxMap) ? dbUhrbMaxMap.get(dbUhrbColumn + suffix) : 0);
            resultMap.put("bMin", Objects.nonNull(dbUhrbMinMap) ? dbUhrbMinMap.get(dbUhrbColumn + suffix) : 0);
            resultMap.put("bAvg", Objects.nonNull(dbUhrbAvgMap) ? dbUhrbAvgMap.get(dbUhrbColumn + suffix) : 0);
            resultMap.put("bTj", Objects.nonNull(dbUhrbTjMap) ? dbUhrbTjMap.get(dbUhrbColumn + suffix) : 0);
            resultMap.put("bCxdz", cxdzStr);
            resultMap.put("bIsNormal", Double.parseDouble(String.valueOf((resultMap.get("bTj")))) > Double.parseDouble(String.valueOf(resultMap.get("bCxdz"))) ? 0 : 1);
            //            C相表数据
            resultMap.put("cMax", Objects.nonNull(dbUhrcMaxMap) ? dbUhrcMaxMap.get(dbUhrcColumn + suffix) : 0);
            resultMap.put("cMin", Objects.nonNull(dbUhrcMinMap) ? dbUhrcMinMap.get(dbUhrcColumn + suffix) : 0);
            resultMap.put("cAvg", Objects.nonNull(dbUhrcAvgMap) ? dbUhrcAvgMap.get(dbUhrcColumn + suffix) : 0);
            resultMap.put("cTj", Objects.nonNull(dbUhrcTjMap) ? dbUhrcTjMap.get(dbUhrcColumn + suffix) : 0);
            resultMap.put("cCxdz", cxdzStr);
            resultMap.put("cIsNormal", Double.parseDouble(String.valueOf((resultMap.get("cTj")))) > Double.parseDouble(String.valueOf(resultMap.get("cCxdz"))) ? 0 : 1);
            resultReport.add(resultMap);
        }
        return resultReport;
    }

    @Override
    public List<Map<String, Object>> selectElectricHarmonics(ReportParams reportParams) {
        List<Map<String, Object>> resultReport = new ArrayList<>();
        List<String> tableColumn = dbFieldInfoMapper.getTableColumn("zk_cxdz", "ihru");
        tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());

        String condition = "type = " + reportParams.getAlarmType();
        if(StrUtil.isNotBlank(reportParams.getTdGuid())){
            condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
        }
//        A表最大最小平均值
        Map<String, Object> dbIhraMaxMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhraMax, dbIhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);

        Map<String, Object> dbIhraMinMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhraMin, dbIhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhraAvgMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhraAvg, dbIhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhraTjMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhraTj, dbIhraColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());

        //        B表最大最小平均值
        Map<String, Object> dbIhrbMaxMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrbMax, dbIhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhrbMinMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrbMin, dbIhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhrbAvgMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrbAvg, dbIhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhrbTjMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrbTj, dbIhrbColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());


        //        C表最大最小平均值
        Map<String, Object> dbIhrcMaxMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrcMax, dbIhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhrcMinMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrcMin, dbIhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhrcAvgMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrcAvg, dbIhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());
        Map<String, Object> dbIhrcTjMap = dbVoltageHarmonicsMapper.selectVoltageHarmonics(dbIhrcTj, dbIhrcColumn, reportParams.getStartTime(), reportParams.getEndTime(), reportParams.getTdGuid());

        for (int i = 1; i < 66; i++) {
            Map<String, Object> resultMap = new HashMap<>();
//            格式化查询结构
            String suffix = String.format("%s", i < 10 ? "0" + i : i);
            resultMap.put("sign", String.format("H%s(%s)", i, i <= 24 ? "A" : "%"));
//            A相表数据

            Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
            Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get("ihru" + i)) ? (Double) cxdzMap.get("ihru" + i) : 0;


            resultMap.put("aMax", Objects.nonNull(dbIhraMaxMap) ? dbIhraMaxMap.get(dbIhraColumn + suffix) : 0);
            resultMap.put("aMin", Objects.nonNull(dbIhraMinMap) ? dbIhraMinMap.get(dbIhraColumn + suffix) : 0);
            resultMap.put("aAvg", Objects.nonNull(dbIhraAvgMap) ? dbIhraAvgMap.get(dbIhraColumn + suffix) : 0);
            resultMap.put("aTj", Objects.nonNull(dbIhraTjMap) ? dbIhraTjMap.get(dbIhraColumn + suffix) : 0);
            resultMap.put("aCxdz", cxdzStr);
            resultMap.put("aIsNormal", Double.parseDouble(String.valueOf((resultMap.get("aTj")))) > Double.parseDouble(String.valueOf(resultMap.get("aCxdz"))) ? 0 : 1);
            //            B相表数据
            resultMap.put("bMax", Objects.nonNull(dbIhrbMaxMap) ? dbIhrbMaxMap.get(dbIhrbColumn + suffix) : 0);
            resultMap.put("bMin", Objects.nonNull(dbIhrbMinMap) ? dbIhrbMinMap.get(dbIhrbColumn + suffix) : 0);
            resultMap.put("bAvg", Objects.nonNull(dbIhrbAvgMap) ? dbIhrbAvgMap.get(dbIhrbColumn + suffix) : 0);
            resultMap.put("bTj", Objects.nonNull(dbIhrbTjMap) ? dbIhrbTjMap.get(dbIhrbColumn + suffix) : 0);
            resultMap.put("bCxdz", cxdzStr);
            resultMap.put("bIsNormal", Double.parseDouble(String.valueOf((resultMap.get("bTj")))) > Double.parseDouble(String.valueOf(resultMap.get("bCxdz"))) ? 0 : 1);
            //            C相表数据
            resultMap.put("cMax", Objects.nonNull(dbIhrcMaxMap) ? dbIhrcMaxMap.get(dbIhrcColumn + suffix) : 0);
            resultMap.put("cMin", Objects.nonNull(dbIhrcMinMap) ? dbIhrcMinMap.get(dbIhrcColumn + suffix) : 0);
            resultMap.put("cAvg", Objects.nonNull(dbIhrcAvgMap) ? dbIhrcAvgMap.get(dbIhrcColumn + suffix) : 0);
            resultMap.put("cTj", Objects.nonNull(dbIhrcTjMap) ? dbIhrcTjMap.get(dbIhrcColumn + suffix) : 0);
            resultMap.put("cCxdz", cxdzStr);
            resultMap.put("cIsNormal", Double.parseDouble(String.valueOf((resultMap.get("cTj")))) > Double.parseDouble(String.valueOf(resultMap.get("cCxdz"))) ? 0 : 1);

            resultReport.add(resultMap);

        }
        return resultReport;
    }

    private static final String maxBaseData = "db_basedatamax";
    private static final String minBaseData = "db_basedatamin";
    private static final String avgBaseData = "db_basedataavg";
    private static final String tjBaseData = "db_basedatatj";

    @Override
    public List<Map<String, Object>> selectDataBase(ReportParams reportParams) {
        List<Map<String, Object>> result = new ArrayList<>();
        String Condition = "timedate between '" + reportParams.getStartTime() + "' and '" + reportParams.getEndTime() + "'";
        String columns = "ROUND(SUM( psta ),2) AS psta ,ROUND(SUM( pstb ),2) AS pstb ,ROUND(SUM( pstc ),2) AS pstc ,ROUND(SUM( plta ),2) AS plta ,ROUND(SUM( pltb ),2) AS pltb ,ROUND(SUM( pltc ),2) AS pltc  ,ROUND(SUM( u0 ),2) AS u0  ,ROUND(SUM( u1 ),2) AS u1  ,ROUND(SUM( u2 ),2) AS u2 ,ROUND(SUM( i0 ),2) AS i0  ,ROUND(SUM( i1 ),2) AS i1  ,ROUND(SUM( i2 ),2) AS i2 ,ROUND(SUM( bphU ),2) AS bphU ,ROUND(SUM( freq ),2) AS freq ";
        if (StrUtil.isNotBlank(reportParams.getTdGuid())) {
            Condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
        }

        List<Map<String, Object>> maxBaseDataList = dbVoltageHarmonicsMapper.selectTableCustomColumn(maxBaseData, columns, Condition);
        List<Map<String, Object>> minBaseDataList = dbVoltageHarmonicsMapper.selectTableCustomColumn(minBaseData, columns, Condition);
        List<Map<String, Object>> avgBaseDataList = dbVoltageHarmonicsMapper.selectTableCustomColumn(avgBaseData, columns, Condition);
        List<Map<String, Object>> tjBaseDataList = dbVoltageHarmonicsMapper.selectTableCustomColumn(tjBaseData, columns, Condition);

        String[] columnsArr = {"psta", "pstb", "pstc", "plta", "pltb", "pltc", "u0", "u1", "u2", "i0", "i1", "i2", "freq","bphU"};
        for (int i = 0; i < columnsArr.length; i++) {
            String column = columnsArr[i];
            Map<String, Object> resultMap = new HashMap<>();
            for (int j = 0; j < maxBaseDataList.size(); j++) {
                Map<String, Object> maxBaseDataMap = maxBaseDataList.get(j);
                Map<String, Object> minBaseDataMap = minBaseDataList.get(j);
                Map<String, Object> avgBaseDataMap = avgBaseDataList.get(j);
                Map<String, Object> tjBaseDataMap = tjBaseDataList.get(j);
                if ("psta,pstb,pstc".contains(column)) {
                    String condition = "type = " + reportParams.getAlarmType();
                    if(StrUtil.isNotBlank(reportParams.getTdGuid())){
                        condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
                    }
                    resultMap.put("columnName", "电压短时闪变");
                    List<String> tableColumn = dbFieldInfoMapper.getTableColumn("zk_cxdz", "pst");
                    
                    tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());
                    List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);
                    Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
                    Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get("pst")) ? (Double) cxdzMap.get("pst") : 0;

                    resultMap.put("aCxdz", cxdzStr);
                }
                if ("plta,pltb,pltc".contains(column)) {
                    String condition = "type = " + reportParams.getAlarmType();
                    if(StrUtil.isNotBlank(reportParams.getTdGuid())){
                        condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
                    }
                    resultMap.put("columnName", "电压长时闪变");
                    List<String> tableColumn = dbFieldInfoMapper.getTableColumn("zk_cxdz", "plt");
                    
                    tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());
                    List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);
                    Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
                    Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get("plt")) ? (Double) cxdzMap.get("plt") : 0;

                    resultMap.put("aCxdz", cxdzStr);
                }
                if ("u0,u1,u2".contains(column)) {
                    String condition = "type = " + reportParams.getAlarmType();
                    if(StrUtil.isNotBlank(reportParams.getTdGuid())){
                        condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
                    }
                    resultMap.put("columnName", "电压序分量");
                    List<String> tableColumn = dbFieldInfoMapper.getTableFinSetColumn("zk_cxdz", "u0,u1,u2");
                    
                    tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());
                    List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);
                    Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
                    Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get(column)) ? (Double) cxdzMap.get(column) : 0;

                    resultMap.put("aCxdz", cxdzStr);
                }

                if("freq".equals(column)){
                    List<String> tableColumn = dbFieldInfoMapper.getTableFinSetColumn("zk_cxdz", "freqU,freqL");
                    tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());
                    String condition = "type = " + reportParams.getAlarmType();
                    if(StrUtil.isNotBlank(reportParams.getTdGuid())){
                        condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
                    }
                    List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn),condition);
                    Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
                    if(cxdzMap != null){
                        Double freqU = (Double)cxdzMap.get("freqU");
                        Double freqL = (Double)cxdzMap.get("freqL");
                        Double aTj = tjBaseDataMap!=null? (Double)tjBaseDataMap.get("aTj"):0;
                        if(freqU != null && freqL != null && aTj != null ){
                            resultMap.put("aIsNormal", aTj >= freqL && freqU >= aTj? 1 : 0);
                            resultMap.put("aCxdz", freqL + "," +freqU);
                        }
                    }else{
                        resultMap.put("aIsNormal", 1);
                        resultMap.put("aCxdz", 0);
                    }

                    resultMap.put("columnName","电压频率(Hz)");
                }
                if("bphU".equals(column)){
                    List<String> tableColumn = dbFieldInfoMapper.getTableColumn("zk_cxdz", "u2_u1");
                    
                    tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());
                    String condition = "type = " + reportParams.getAlarmType();
                    if(StrUtil.isNotBlank(reportParams.getTdGuid())){
                        condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
                    }
                    List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);
                    Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
                    Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get("u2_u1")) ? (Double) cxdzMap.get("u2_u1") : 0;
                    resultMap.put("columnName","三相电压不平衡度(%)");
                    resultMap.put("aCxdz", cxdzStr);
                }

                if ("i0,i1,i2".contains(column)) {
                    resultMap.put("columnName", "电流序分量");
                    List<String> tableColumn = dbFieldInfoMapper.getTableFinSetColumn("zk_cxdz", "i0,i1,i2");
                    
                    tableColumn = tableColumn.stream().map(m->"ROUND(SUM("+ m + "),2) as "+m).collect(Collectors.toList());
                    String condition = "type = " + reportParams.getAlarmType();
                    if(StrUtil.isNotBlank(reportParams.getTdGuid())){
                        condition += " and tdGuid = '" + reportParams.getTdGuid() + "'";
                    }
                    List<Map<String, Object>> cxdzList = dbVoltageHarmonicsMapper.selectTableCustomColumn("zk_cxdz", String.join(",", tableColumn), condition);
                    Map<String, Object> cxdzMap = cxdzList.size() > 0 ? cxdzList.get(0) : null;
                    Double cxdzStr = cxdzMap != null && Objects.nonNull(cxdzMap.get(column)) ? (Double) cxdzMap.get(column) : 0;

                    resultMap.put("aCxdz", cxdzStr);
                }


                if ("u0,i0".contains(column)) {
                    resultMap.put("name", "零序");
                }
                if ("u1,i1".contains(column)) {
                    resultMap.put("name", "正序");
                }
                if ("u2,i2".contains(column)) {
                    resultMap.put("name", "负序");
                }
                if ("psta,pstb,pstc,plta,pltb,pltc".contains(column)) {
                    resultMap.put("name", column);
                }
                resultMap.put("aMax", maxBaseDataMap != null ? maxBaseDataMap.get(column) : 0);
                resultMap.put("aMin", minBaseDataMap != null ? minBaseDataMap.get(column) : 0);
                resultMap.put("aAvg", avgBaseDataMap != null ? avgBaseDataMap.get(column) : 0);
                resultMap.put("aTj", tjBaseDataMap != null ? tjBaseDataMap.get(column) : 0);
                if(!column.equals("freq")){
                    resultMap.put("aIsNormal", Double.parseDouble(String.valueOf((resultMap.get("aTj")))) > Double.parseDouble(String.valueOf(resultMap.get("aCxdz"))) ? 0 : 1);
                }
            }
            result.add(resultMap);
        }
        return result;
    }
}
